package com.vnt;

import java.io.IOException;

import javax.microedition.lcdui.Image;

import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.core.KuixConstants;
import org.kalmeo.kuix.core.model.DataProvider;
import org.kalmeo.kuix.widget.ListItem;
import org.kalmeo.kuix.widget.Picture;
import org.kalmeo.kuix.widget.Screen;
import org.kalmeo.kuix.widget.ScrollPane;
import org.kalmeo.kuix.widget.Text;
import org.kalmeo.kuix.widget.Widget;
import org.kalmeo.util.frame.Frame;
import com.ilove.core.*;
import com.ilove.frame.*;
public class ContentListFrame implements Frame {
	public static ContentListFrame instance = new ContentListFrame();
	public int id;// 0 text data from vas, 1 ringtone, 2 game ,3 wallpaper
	public String code;
	public String name;
	private int currPage = 1;
	private int totalPage = 1;
	public String[] vecContent = null;
	boolean isShort = false;

	// MainFrame's Screen
	private Screen screen;

	// The Pad that hold all shopLists
	private final Pad pad = new Pad();

	/**
	 * Show the screen
	 */
	public void showScreen() {
		if (screen != null) {
			screen.setCurrent();
		}
	}

	public void onAdded() {
		try {
			System.out.println("Content list frame on add");
			String[] contentlist = pad.getContentList(code,id,name); 
			if (contentlist != null && contentlist.length > 0) {
				// Open the Pad
				pad.getContentIndex(contentlist, name);
				if(name == "XOSO" || name == "Truyen")
					screen = Kuix.loadScreen(ILOVEContants.SUBINDEX_SCREEN, pad);
				else
					screen = Kuix.loadScreen(ILOVEContants.CONTENTLIST_SCREEN, pad);
				isShort = false;
				screen.setCurrent();
			}
			else {
				System.out.println("Content list is null");
				ContentFrame.instance.id = id;
				ContentFrame.instance.contentId = code;
				ContentFrame.instance.contentName = name;
				// ContentFrame.instance.pop = pop;
				Kuix.getFrameHandler().pushFrame(ContentFrame.instance);
			}
			
		} catch (Exception e) {
			Kuix.alert(e);
			e.printStackTrace();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.kalmeo.util.frame.Frame#onMessage(java.lang.Object,
	 *      java.lang.Object[])
	 */
	
	public boolean onMessage(Object identifier, Object[] arguments) {
		if ("askQuit".equals(identifier)) {
			// show askQuit box wich call onMessage method with "quit" name and
			// null arguments
			Kuix.alert(Kuix.getMessage("ASK_QUIT"),
					KuixConstants.ALERT_QUESTION | KuixConstants.ALERT_YES
							| KuixConstants.ALERT_NO, "!exit", null);
			return false;
		} else if ("next".equals(identifier)) {
			Widget focusedWidget = screen.getFocusManager().getFocusedWidget();
			if (focusedWidget instanceof ListItem) {
				DataProvider dataprovider = ((ListItem) focusedWidget)
						.getDataProvider();
				if (dataprovider != null) {
					SubIndexList subIndexList = (SubIndexList) dataprovider;
					try {
						ContentFrame.instance.id = id;
						System.out.println(id);
						if(name=="Truyen")
							ContentFrame.instance.contentId = Integer.toString(subIndexList.id);
						else
							ContentFrame.instance.contentId = code;
						System.out.println("ContentFrame.instance.contentId="+ContentFrame.instance.contentId); 
						if(id==4){
							ContentFrame.instance.contentName = subIndexList.name.substring(0,2);
						}else
							ContentFrame.instance.contentName = name;
						System.out.println("ContentFrame.instance.contentName="+ContentFrame.instance.contentName); 
						Kuix.getFrameHandler().pushFrame(ContentFrame.instance);
					} catch (Exception e) {
					}
				}
			}
		} else if ("back".equals(identifier)) {
			Kuix.getFrameHandler().removeFrame(instance);
			return false;
		} else if ("arrLeft".equals(identifier) && isShort) {
			if (currPage > 1)
				currPage--;
			changePageValue(currPage);
		} else if ("arrRight".equals(identifier) && isShort) {
			if (currPage < totalPage)
				currPage++;
			changePageValue(currPage);
		} else if ("about".equals(identifier)) {
			Kuix.getFrameHandler().pushFrame(AboutFrame.instance);
			return false;
		}
		return true;
	}

	public void changePageValue(int page) {
		Text textPage = (Text) screen.getWidget("textPage");
		textPage.setText(page + "/" + totalPage);
		Picture pictureLeft = (Picture) screen.getWidget("pic-left");
		Picture pictureRight = (Picture) screen.getWidget("pic-right");
		try {
			if (page == 1) {
				pictureLeft.setImage(Image.createImage("/img/left-dis.png"));
			} else {
				pictureLeft.setImage(Image.createImage("/img/left.png"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			if (page == totalPage) {
				pictureRight.setImage(Image.createImage("/img/right-dis.png"));
			} else {
				pictureRight.setImage(Image.createImage("/img/right.png"));
			}

			ScrollPane scrollPane = (ScrollPane) screen.getWidget("SP");
			scrollPane.setHorizontal(false);
			pad.setContent((String) vecContent[page - 1]);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see org.kalmeo.util.frame.Frame#onAdded()
	 */

	public void onRemoved() {
		// show MainFrame screen
		try {
			SubIndexFrame.instance.showScreen();
			screen.cleanUp();
			screen = null;
		} catch (Exception e) {
		}

	}
}
